<template>
  <div>
    <div style="margin-top: 2%;text-align:center;">
      <el-select v-model="form.academyId"
                 style="width: 18%;margin-right: 1%"
                 clearable filterable
                 placeholder="请选择学院">
        <el-option v-for="item in academy" :key="item.academyId"
                   :label="item.academyId +' - '+ item.academyName"
                   :value="item.academyId">
        </el-option>
      </el-select>
      <el-input style="width: 24%;margin-right: 1%" placeholder="请输入待查找的专业名称(可指定学院)"
                v-model="form.professionName"></el-input>
      <el-button @click="findProfession()" style="font-size: 20px" type="primary">立即查找</el-button>
      <el-button @click="addProfession()" style="font-size: 20px" type="primary">点击此处新增专业</el-button>
    </div>

    <template style="margin-top: 2%">
      <el-table :data="pageInfo.list" style="width: 100%">
        <el-table-column label="" width="160">
        </el-table-column>
        <el-table-column label="专业编号" width="240" prop="professionId" sortable>
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{scope.row.professionId}}</span>
          </template>
        </el-table-column>
        <el-table-column label="专业名称" width="600" prop="professionName" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.professionName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属学院" width="400" prop="academyId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.academyName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="small"
                style="background-color: deepskyblue;font-size: 16px"
                @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-bottom: 1%;"></div>
      <el-pagination
          style="text-align: center;"
          background
          layout="total,prev, pager, next"
          :total="pageInfo.total"
          :page-size="pageInfo.pageSize"
          @current-change="handleCurrentChange">
      </el-pagination>
    </template>

    <el-drawer title="新增专业" :visible.sync="add" :direction="addDirection">
      <el-card style="margin:0 auto;width: 96%;">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="20%">
          <el-form-item  label="专业编号">
            <el-input-number v-model="ruleForm.professionId" :min="1" :max="999999"></el-input-number>
          </el-form-item>
          <el-form-item label="专业名称" prop="professionName">
            <el-input v-model="ruleForm.professionName" placeholder="例如:软件工程" style="width: 60%"></el-input>
          </el-form-item>
          <el-form-item label="选择学院" prop="academyId">
            <el-select v-model="ruleForm.academyId"
                       clearable filterable
                       placeholder="请选择学院">
              <el-option v-for="item in academy" :key="item.academyId"
                         :label="item.academyId +' - '+ item.academyName"
                         :value="item.academyId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即新增</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-drawer>

    <el-drawer title="修改专业" :visible.sync="modify" :direction="modifyDirection">
      <el-card style="margin:0 auto;width: 96%;">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="20%">
          <el-form-item  label="专业编号">
            <el-input
                v-model="ruleForm.professionId"
                style="width: 40%"
                :disabled="true">
            </el-input>
          </el-form-item>
          <el-form-item label="专业名称" prop="professionName">
            <el-input v-model="ruleForm.professionName" placeholder="例如:软件工程" style="width: 60%"></el-input>
          </el-form-item>
          <el-form-item label="选择学院" prop="academyId">
            <el-select v-model="ruleForm.academyId"
                       clearable filterable
                       placeholder="请选择学院">
              <el-option v-for="item in academy" :key="item.academyId"
                         :label="item.academyId +' - '+ item.academyName"
                         :value="item.academyId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      add: false,
      addDirection: 'rtl',
      modify: false,
      modifyDirection: 'rtl',
      academy:[],
      currPage: 1,
      pageInfo:{},
      form:{
        professionName: '',
        academyId: '',
      },
      ruleForm:{
        professionId: '',
        professionName: '',
        academyId: '',
      },
      rules: {
        professionName: [
          { required: true, message: '请输入专业名称', trigger: 'blur' },
          { min: 2, max: 30, message: '请输入2 - 30个字符!', trigger: 'blur' }
        ],
        academyId: [
          { required: true, message: '请选择学院', trigger: 'blur' },
        ]
      }
    };
  },
  created() {
    this.academyInit();
    this.getData();
  },
  methods: {
    submitForm(formName){ // 新增/修改专业
      this.$refs[formName].validate((valid) => {//校验格式
        if (valid) {//格式校验通过
          let path;
          if(this.add === true) path = 'http://localhost:8081/profession/insertOne';
          else if(this.modify === true) path = 'http://localhost:8081/profession/updateOne';
          else path = '';
          this.$ajax.post(path,this.ruleForm).then(res=>{
            if(res.data.status === 0){
              this.$message.success(res.data.message);
              this.add = false;
              this.modify = false;
              this.getData();
            }
            else{
              this.$message.error(res.data.message);
            }
          })
        }
      });
    },
    handleEdit(row){
      this.ruleForm.professionId = row.professionId;
      this.ruleForm.professionName = row.professionName;
      this.ruleForm.academyId = row.academyId;
      this.modify = true;
    },
    addProfession(){
      this.ruleForm.professionId = '';
      this.ruleForm.professionName = '';
      this.ruleForm.academyId = '';
      this.add = true;
    },
    handleCurrentChange(val){
      this.currPage = val;
      this.getData();
    },
    findProfession(){
      this.currPage = 1;
      this.getData();
    },
    getData(){
      let path = `http://localhost:8081/profession/getProfessionList/${this.currPage}`;
      this.$ajax.post(path,this.form).then(res=>{
        this.pageInfo = res.data;
      })
    },
    academyInit(){
      let path = 'http://localhost:8081/academy/selectAcademyCanUse';
      this.$ajax.post(path).then(res=>{
        this.academy = res.data.list
      })
    },
  }
};
</script>

<style scoped>
span{
  font-size: 18px;
}
</style>